'use strict';
const dayjs = require("dayjs");
const getNumberValue = function (value) {
    let numberValue = Number(value);
    return isNaN(numberValue) ? 0 : numberValue;
}
module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE, DECIMAL } = app.Sequelize;
  const Ship = app.model.define(
      'Ship',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            name: STRING(255),
              type: INTEGER,
              count: {
                  type: DECIMAL(10,1),
                  get() {
                      return getNumberValue(this.getDataValue('count'))
                  }
              },
            desc: TEXT,
            content: TEXT,
            post_url: STRING(255),
            images: TEXT,
            duration: {
                type: DECIMAL(10,1),
                get() {
                    return getNumberValue(this.getDataValue('duration'))
                }
            },
            min_duration: {
                type: DECIMAL(10,1),
                get() {
                    return getNumberValue(this.getDataValue('min_duration'))
                }
            },
            location: STRING(255),
            code: STRING(255),
            min_count: {
                type: DECIMAL(10,1),
                get() {
                    return getNumberValue(this.getDataValue('min_count'))
                }
            },
            max_count: {
                type: DECIMAL(10,1),
                get() {
                    return getNumberValue(this.getDataValue('max_count'))
                }
            },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
          start_time: STRING(255),
          end_time: STRING(255),
          price: {
              type: DECIMAL(10, 2),
              get() {
               return getNumberValue(this.getDataValue('price'))
              }
          },
          child_price: {
              type: DECIMAL(10, 2),
              get() {
                  return getNumberValue(this.getDataValue('child_price'))
              }
          },
      },
      {
          tableName: 'ship',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
    return Ship
};
